﻿Public Class DRUGUSETEMPLATECLASS

    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection

    Private DRGTMPID As Integer = Nothing
    Private DRGCODE As String = Nothing
    Private USEID As String = Nothing
    Private TIMEID1 As String = Nothing
    Private TIMEID2 As String = Nothing
    Private WRNID1 As String = Nothing
    Private WRNID2 As String = Nothing
    Private STATUS = Nothing
    Private selectDrglbltemplate As String = "SELECT a.`drgtmpid` AS 'รหัส template การใช้ยา', b.`drgcode` AS 'รหัสยา', b.`genericname` AS 'ชื่อสามัญ', b.`tradename` AS 'ชื่อทางการค้า', c.`ugid` AS 'รหัสการใช้ยา', c.`dusename` AS 'วิธีการใช้ยา(ไทย)', c.`dusename_en` AS 'วิธีการใช้ยา(อังกฤษ)', d.`dtcode` AS 'รหัสเวลาการใช้ยา(1)', d.`dtname` AS 'เวลาในการใช้ยา(ไทย1)', d.`dtname_en` AS 'เวลาในการใช้ยา(อังกฤษ1)', e.`dtcode` AS 'รหัสเวลาการใช้ยา(2)', e.`dtname` AS 'เวลาในการใช้ยา(ไทย2)', e.`dtname_en` AS 'เวลาในการใช้ยา(อังกฤษ2)', f.`wrncod` AS 'รหัสคำเตือน(1)', f.`wrnname` AS 'คำเตือน(ไทย1)', f.`wrnname_en` AS 'คำเตือน(อังกฤษ1)', g.`wrncod` AS 'รหัสคำเตือน(2)', g.`wrnname` AS 'คำเตือน(ไทย2)', g.`wrnname_en` AS 'คำเตือน(อังกฤษ2)', CONCAT_WS(' ', c.dusename, d.dtname, e.dtname, f.wrnname, g.wrnname) AS 'วิธีการใช้(ไทย)', CONCAT_WS(' ', c.dusename_en, d.dtname_en, e.dtname_en, f.wrnname_en, g.wrnname_en) AS 'วิธีการใช้(อังกฤษ)', a.status AS 'สถานะการเปิดใช้งาน' FROM drglbltemplate a LEFT JOIN drugitem b ON a.drgcode = b.drgcode LEFT JOIN masdrguse c ON a.useid = c.ugid LEFT JOIN masdrgtime d ON a.timeid1 = d.dtcode LEFT JOIN masdrgtime e ON a.timeid2 = e.dtcode LEFT JOIN masdrgwrn f ON a.wrnid1 = f.wrncod LEFT JOIN masdrgwrn g ON a.wrnid2 = g.wrncod"

    Public Property DRGTMPID_() As Integer
        Get
            Return DRGTMPID
        End Get
        Set(value As Integer)
            DRGTMPID = value
        End Set
    End Property

    Public Property DRGCODE_ As String
        Get
            Return DRGCODE
        End Get
        Set(value As String)
            DRGCODE = value
        End Set
    End Property

    Public Property USEID_ As String
        Get
            Return USEID
        End Get
        Set(value As String)
            USEID = value
        End Set
    End Property

    Public Property TIMEID1_ As String
        Get
            Return TIMEID1
        End Get
        Set(value As String)
            TIMEID1 = value
        End Set
    End Property

    Public Property TIMEID2_ As String
        Get
            Return TIMEID2
        End Get
        Set(value As String)
            TIMEID2 = value
        End Set
    End Property

    Public Property WRNID1_ As String
        Get
            Return WRNID1
        End Get
        Set(value As String)
            WRNID1 = value
        End Set
    End Property

    Public Property WRNID2_ As String
        Get
            Return WRNID2
        End Get
        Set(value As String)
            WRNID2 = value
        End Set
    End Property

    Property STATUS_ As Boolean
        Get
            Return STATUS
        End Get
        Set(value As Boolean)
            If value = True Then
                STATUS = 1
            Else
                STATUS = 0
            End If
        End Set
    End Property

    Public Property selectDrglbltemplate_ As String
        Get
            Return selectDrglbltemplate
        End Get
        Set(value As String)

        End Set
    End Property

    Public Function insertDrugUseTemplateClass() As Boolean
        If connect.GetTable("SELECT drgtmpid FROM drglbltemplate WHERE drgcode = '" & DRGCODE & "' AND useid = '" & USEID & "' AND timeid1 = '" & TIMEID1 & "' AND timeid2 = '" & TIMEID2 & "' AND wrnid1 = '" & WRNID1 & "' AND wrnid2 = '" & WRNID2 & "'").Rows.Count > 0 Then
            MsgBox("ไม่สามารถเพิ่มรูปแบบการใช้ยานี้ได้อีก เนื่องจากมีรูปแบบนี้ในระบบอยู่แล้ว")
        Else
            Try
                connect = CONDB.NewConnection()
                'connectlocal = conlocal.NewConnection()

                connect.ExecuteNonQuery("INSERT INTO drglbltemplate (drgcode, useid, timeid1, timeid2, wrnid1, wrnid2, status) VALUES ('" & DRGCODE & "', '" & USEID & "', '" & TIMEID1 & "', '" & TIMEID2 & "', '" & WRNID1 & "', '" & WRNID2 & "', '" & STATUS & "')")
                'connectlocal.ExecuteNonQuery("INSERT INTO MASCOUNTRY ([CODECOUNTRY], [COUNTRY]) VALUES (" & countryCODECOUNTRY & ", '" & countryCOUNTRY & "');")
                MsgBox("ท่านได้ทำการเพิ่มรูปแบบการใช้ยา และคำเตือนเรียบร้อยแล้ว")

                Return True
            Catch ex As Exception
                MsgBox(vbCrLf & ex.Message)
                Return False
            End Try
            'connectlocal.Dispose()
            connect.Dispose()
        End If
    End Function

    Public Function editDrugUseTemplateClass() As Boolean
        If connect.GetTable("SELECT DRGTMPID FROM drglbltemplate WHERE drgcode = '" & DRGCODE & "' AND useid = '" & USEID & "' AND timeid1 = '" & TIMEID1 & "' AND timeid2 = '" & TIMEID2 & "' AND wrnid1 = '" & WRNID1 & "' AND wrnid2 = '" & WRNID2 & "'").Rows.Count > 0 Then
            MsgBox("ไม่สามารถแก้ไขรูปแบบการใช้ยานี้ได้ เนื่องจากไม่ได้มีการเปลี่ยนแปลงข้อมูล")
        Else
            Try
                connect = CONDB.NewConnection()
                'connectlocal = conlocal.NewConnection()

                connect.ExecuteScalar("UPDATE drglbltemplate SET drgcode = '" & DRGCODE & "', useid = '" & USEID & "', timeid1 = '" & TIMEID1 & "', timeid2 = '" & TIMEID2 & "', wrnid1 = '" & WRNID1 & "', wrnid2 = '" & WRNID2 & "', status = '" & STATUS & "' WHERE drgtmpid = '" & DRGTMPID & "'")
                'connectlocal.ExecuteNonQuery("INSERT INTO MASCOUNTRY ([CODECOUNTRY], [COUNTRY]) VALUES (" & countryCODECOUNTRY & ", '" & countryCOUNTRY & "');")
                MsgBox("ท่านได้ทำการแก้ไขรูปแบบการใช้ยา และคำเตือนเรียบร้อยแล้ว")

                Return True
            Catch ex As Exception
                MsgBox(vbCrLf & ex.Message)
                Return False
            End Try
            'connectlocal.Dispose()
            connect.Dispose()
        End If
    End Function
End Class
